<?php

namespace App\Admin\Controllers;

use App\Models\Sgb;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Shijiindex as sj;
use Encore\Admin\Widgets\Table;
use MongoDB\BSON\UTCDateTime;
//use Shuai\Phpwx\Phpwx as Api;
use App\Admin\Api;
class SgbController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '读歌本内容管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Sgb());

        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->in('ShijiIndex', '所属诗集')->multipleSelect($this->sjOption());

            $filter->like('Mulu', '标题');

            $filter->like('Xuhao','序号');

        });

        $self = $this;

        $grid->column('mulu', __('Mulu'))->sortable();
        $grid->column('shijiIndex', __('ShijiIndex'))->using($this->sjOption())->sortable();
        $grid->column('xuhao', __('Xuhao'))->sortable();
        $grid->column('isad', __('Isad'))->bool();
        $grid->column('isopern', __('Isopern'))->bool();
        $grid->column('lyrice', __('Lyrice'))->limit(30)->width(100);
        $grid->column('created_at', __('Created at'))->sortable()->display(function ($created_at) use($self){
           return $self->dataFormat($created_at);
        });
        $grid->column('updated_at', __('Updated at'))->sortable()->display(function ($updated_at) use($self){
            return $self->dataFormat($updated_at);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

        $data = Sgb::findOrFail($id);

        $show = new Show($data);

        $dmpath = "http://od.qumei.com/sgb/dmt/ad/" .md5($data->xuhao.$data->mulu);

        $show->field('_id', __(' id'));
        $show->field('isad', __('Isad'));

        if($data->isad){
            $show->音频()->unescape()->as(function ()use($dmpath){
                return "<audio controls src='{$dmpath}.mp3'/>";
            });
        }
        $show->field('lyrice', __('Lyrice'));
        $show->field('mulu', __('Mulu'));
        $show->field('shijiIndex', __('ShijiIndex'));
        $show->field('xuhao', __('Xuhao'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('isopern', __('Isopern'));
        if($data->isopern){
            $show->歌谱()->unescape()->as(function () use($dmpath){
                return "<img class='img-responsive' src='".$dmpath.".jpg'/>";
            });
        }




        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Sgb());

        $form->text('mulu', __('Mulu'))->rules('required');
        $form->select('shijiIndex', __('ShijiIndex'))->options($this->sjOption())->rules('required');
        $form->number('xuhao', __('Xuhao'))->rules('required');

        $form->switch('isad', __('Isad'));
        $form->switch('isopern', __('Isopern'));
        $form->textarea('lyrice', __('Lyrice'));
        if($form->isCreating()){
            $form->text('_id', __('_Id'))->value(md5(time().rand(1000,9999)));

        }else{
            $form->hidden('_id', __('_Id'));
        }



        $form->footer(function ($footer){
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
//            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
//            $footer->disableCreatingCheck();
        });




        //保存后回调
        $form->saved(function (Form $form) {

            $api = new Api();
            $collection = 'zanmeidb';
            $data = json_decode($form->model(),JSON_UNESCAPED_SLASHES);
            $data['xuhao'] = (int)$data['xuhao'];
            $data['shijiIndex'] = (int)$data['shijiIndex'];

            /**
             * 新增条目数操作
             */
            if($form->isCreating()){
                $data = Array($data);
                $api->addItem($collection,$data);
            }elseif ($form->isEditing()){

                $api->updateItem($collection,$data);
            }


        });


        return $form;
    }

    public function sjOption(){
        $data = sj::get();

        foreach ($data as $val){
            $sp[$val->id]=$val->title;;
        }
        return $sp;
    }

    /**
     * 格式化时间
     * @param $created_at UTCDateTime UTC时间
     * @return false|string
     */
    public function dataFormat($created_at){

        if($created_at==''){
            return '';
        }

        $unix = strtotime($created_at);

        return date('Y-m-d H:i:s',$unix);
    }

}
